<template>
  <div class="container">
    <div class="u-title f-mtp10">
      <h2>{{title}}</h2>
    </div>

    <div class="u-content mn flex flex-column flex-center">
      <div class="item">
        <label for>分类标题</label>
        <el-input class="f-w250" v-model="name" placeholder="分类标题"></el-input>
      </div>
      <div class="item">
        <label for>分类图标</label>
        <div class="f-w250">
          <div class="u-upload">
            <input
              type="file"
              @change="upfile"
              name="file"
              id="file"
              accept="image/png, image/jpeg, image/gif, image/jpg"
              class="u-upload-file"
            >
            <label for="file" class="btn btn-success">
              <img v-if="iconUrl" :src="iconUrl" alt="">
              <i class='el-icon-plus' v-else></i>
            </label>
          </div>
          <p class="upload-tip">只能上传jpg/png格式文件，文件不能超过50kb</p>
        </div>
      </div>
      <div class="item">
        <label for>分类备注</label>
        <el-input class="f-w250" v-model="remark" placeholder="分类备注"></el-input>
      </div>
      <div class="btn-box f-ptp20">
        <el-button class="f-w250 s-bg-green s-cfff" @click="submit">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from 'axios';
export default {
  name: "AddClassify",
  data() {
    return {
      title: "添加分类",
      iconUrl: "",
      name: "",  // 分类标题
      remark: "",
      id: null,
    };
  },
  created() {
    if (this.$route.params.id) {
      this.id = this.$route.params.id;
      this.title = "编辑分类";
      this.getDetail();
    }
  },
  methods: {
    getDetail() {
      this.$store.dispatch('GET_CASE_TYPE_DETAIL', {
        id: this.id
      }).then(res => {
        console.log('res:::', res);
        if(res.data.retCode == 0) {
          this.name = res.data.data.name;
          this.iconUrl = res.data.data.icon;
          this.remark = res.data.data.remark;
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      })
    },
    // 提交按钮点击事件 
    submit() {
      if(this._utils.isEmpty(this.name)) {
        this.$message.warning('请输入分类名称！');
        return;
      }
      if(this._utils.isEmpty(this.iconUrl)) {
        this.$message.warning('请上传分类图标！');
        return;
      }
      if(this._utils.isEmpty(this.remark)) {
        this.$message.warning('请输入备注！');
        return;
      }
      this.handleCaseType();
    },
    // 分类管理-新增/更新分类
    handleCaseType() {
      this.$store.dispatch('HANDLE_CASE_TYPE', {
        id: this.id,
        name: this.name,
        icon: this.iconUrl,
        remark: this.remark
      }).then(res => {
        if(res.data.retCode == 0) {
          this.$message.success('提交成功！');
          this.$router.push('/home/classify');
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      })
    },
    // 上传图片
    upfile(e) {
      const files = e.target.files;
      console.log("files:::", files);
      console.log("files[0]:::", files[0]);
      const form = new FormData();
      form.append("file", files[0]);
      form.append("dir", "lawfirm");
      form.append("type", "1");
   
      this.$store.dispatch("UPLOAD_FILE", form).then(res => {
        console.log("res::::up:::", res);
        if(res.data.retCode == 0) {
          this.$message.success('上传成功！');
          this.iconUrl = res.data.data;
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

.upload-tip {
  font-size: 12px;
  color: #999;
  margin-top: 10px;
  line-height: 24px;
}

.container {
  width: 560px;
  /* margin: 0 auto; */
}
.mn {
  padding: 30px 0 50px 0;
}
.item {
  display: flex;
  margin-bottom: 10px;
}
.item > label {
  display: inline-block;
  width: 80px;
  line-height: 40px;
  text-align: right;
  margin-right: 10px;
}
.btn-box {
  padding-left: 80px;
}
</style>
